/*
 * Filename: g:\espidf_workspace\idf_S3_T-Watch\main\audio\audio_player.h
 * Path: g:\espidf_workspace\idf_S3_T-Watch\main\audio
 * Created Date: Monday, June 23rd 2025, 11:18:25 am
 * Author: sss
 *
 * Copyright (c) 2025 Your Company
 */
#pragma once
#include "driver/i2s.h"
#include "driver/i2s_types.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "freertos/ringbuf.h"

// MAX98357A
#define AUDIO_PLAYER_IIS_BCK (GPIO_NUM_48)
#define AUDIO_PLAYER_IIS_WS (GPIO_NUM_15)
#define AUDIO_PLAYER_IIS_DOUT (GPIO_NUM_46)
#define AUDIO_PLAYER_PORT (I2S_NUM_0)

class AUDIO_PLAYER
{
private:
    AUDIO_PLAYER(/* args */);
    ~AUDIO_PLAYER();
    /* data */
    bool i2s_inited = false;// whether i2s is initialized
    bool playing = false;// whether audio is playing
    RingbufHandle_t buf_handle;
    size_t ring_buffer_size = 1024 * 10; // ring buffer size in  uint16_t units
    void init_i2s();
    void deinit_i2s();
    void clear_buffer();
    void play();//播放缓冲区数据
public:
    static AUDIO_PLAYER &get_instance()
    {
        static AUDIO_PLAYER instance;
        return instance;
    };
    AUDIO_PLAYER(AUDIO_PLAYER const &) = delete;
    AUDIO_PLAYER& operator=(AUDIO_PLAYER const &) = delete;
    
    void play_url(const char *audio_url);// play audio from url https://www.example.com/audio.mp3 播放网络音频 获取网络音频  放入缓冲区  mp3可能需要解码 pcm 不需要
    size_t push_audio_data(void *data, size_t len);// play audio from buffer 将音频数据放入缓冲区待播放 返回实际放入的长度

    void stop_audio();
    void pause_audio();
    void resume_audio();
};

